package com.ncs.nc.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.MessageProperties;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class MqRabbitProducer {
    //交换器名称
    private static final String EXCHANGE="mq.simple.exchange";
    //队列名称
    private static final String QUEUE="mq.simple.queue";
    //路由键
    private static final String ROUTING_KEY="mq.simple.routing.key";

    public static void main(String[] args) throws IOException, TimeoutException {
        //创建连接
        Connection con= MqRabbitConnectionUtil.getConnection();
        //创建信道
        Channel channel=con.createChannel();

        //声明一个类型为"direct",持久化，非自动删除的交换器
        channel.exchangeDeclare(EXCHANGE,"direct",true,false,null);
        //声明一个持久化的，排他的，非自动删除的队列
        channel.queueDeclare(QUEUE,true,false,false,null);
        //绑定交换器和队列
        channel.queueBind(QUEUE,EXCHANGE,ROUTING_KEY);

        //消息
        String message="这是入门RabbitMQ的消息:Hello Rabbit~";
        //发送消息
        channel.basicPublish(EXCHANGE,ROUTING_KEY, MessageProperties.PERSISTENT_TEXT_PLAIN,message.getBytes());

        System.out.println("生产者成功发送消息："+message);

        //关闭信道
        channel.close();
        //关闭连接
        con.close();

    }
}
